package com.study.system.user.controller;


import com.alibaba.fastjson.JSON;
import com.study.system.BaseServlet;
import com.study.system.log.domain.UserLog;
import com.study.system.log.service.Impl.LoginServiceImpl;
import com.study.system.user.domain.LoginUser;
import com.study.system.user.domain.User;
import com.study.system.user.domain.UserDept;
import com.study.system.user.service.UserService;
import com.study.system.user.service.impl.UserServiceImpl;
import com.study.system.utils.JsonData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Service;
import java.io.BufferedReader;
import java.util.List;

/**
 * @Author 669
 * @CreateDate 2022/4/19 19:01
 * @Version 1.0
 */
@WebServlet("/login/*")
public class LoginController extends BaseServlet {

    //判断用户名是否合法
    public void checkUsernameServlet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            //获取登录信息
            String username = req.getParameter("username");
            //查询用户是否存在
            UserService service = new UserServiceImpl();
            User user = service.selectByUsername(username);
            //用户存在响应为ture
            if (user != null) {
                resp.getWriter().write("true");
            } else {
                //用户不存在响应为0
                resp.getWriter().write("false");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //判断用户是否存在
    public void checkUsernameAndPasswordServlet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            //获取登录信息
            String username = req.getParameter("username");
            String password = req.getParameter("password");

            //判断参数
            String stauts = req.getParameter("status");

            //查询数据
            UserService service = new UserServiceImpl();
            User user = service.selectByUsernameAndPassword(username, password);

            if (user != null) {
                if ("1".equals(stauts)) {
                    //记住密码
                    Cookie cookie_username = new Cookie("username", username);
                    Cookie cookie_password = new Cookie("password", password);
                    //设置存活时间:1天

                    cookie_username.setMaxAge(60 * 60 * 24); //易阅读，需程序计算
                    cookie_password.setMaxAge(60 * 60 * 24); //易阅读，需程序计算
                    resp.addCookie(cookie_password);
                    resp.addCookie(cookie_username);
                }
                HttpSession session = req.getSession();

                //设置用户信息
                UserDept UserAndDept = new UserServiceImpl().selectallUserAndDeptByUsername(username);
                System.out.println(UserAndDept.toString());
                //绑定信息
                session.setAttribute("user", UserAndDept);

                int dept_id = user.getDeptid();
                if (dept_id == 0) {
                    req.getRequestDispatcher("/jsp/admin/admin_index.jsp").forward(req, resp);
                }
                if (dept_id != 0) {
                    req.getRequestDispatcher("/jsp/user/user_index.jsp").forward(req, resp);
                }

            } else {
                //密码错误重新登陆
                HttpSession session = req.getSession();
                //设置错误信息
                session.setAttribute("login_msg1", true);
                req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

